#ifndef F_CASGIC_h
#define F_CASGIC_h

// ***********************************************************************

// class EDROOMTopContext

// ***********************************************************************



class EDROOMTopContext {

protected:

enum EDROOMCASGISignal { EDROOMSignalTimeout, EDROOMSignalDestroy, SKeepOnComm, SToStandBy, STMToTransmit, SToComm, SInStandBy, STMTransmited, SCommConfigured, STCReceived, SCommInterrupted, EmuGSDoublePackList, STMTCPackList, STMDoublePackList, SFinalTCPackets, STCPackList, SNoAnswer, SRxBurst, SRxDataBurst, SAnswer, STxBurst, STxDataBurst };


	friend class CASGI;

	CASGI &EDROOMcomponent;

	CEDROOMMessage * &Msg;

	CEDROOMMessage * &MsgBack;

	//Ports

	CEDROOMInterface & PlannerSGIControl;
	CEDROOMInterface & SgiTMTCServer;
	CEDROOMInterface & SGIComm;
	CEDROOMTimingInterface & SATReadyTimer;
	CEDROOMTimingInterface & TxRxBurstTimer;

	enum edroomStateId{I, StandBy, SendingSATReady, SATControl, SenTMTCDelivery, CommInterrupted};

	enum IdTrans{Init, ToComm, TxSATReadyRxBurst, TxSATReadyRxBurst_ToSATControl, TxSATReadyRxBurst_ToSenTMTCDelivery, TxSATReadyRxBurst_AnswerFail, TxSATReadyRxBurst_LinkLost, KeepOnComm, ToStandBy,  EDROOMMEMORIA };

	//Constantes



	//Variables

	word32_t &VAcks;
	byte_t &VAltBit;
	bool_t &VAlternate;
	CDBurst &VBurst;
	CDDataBurst &VDataBurst;
	CPackListHandler &VFreePackList;
	bool_t &VIsSatControl;
	bool_t &VIsSenTMTCDelivery;
	bool_t &VLastTCBurst;
	bool_t &VLastTMBurst;
	bool_t &VLinkLost;
	word32_t &VSizeProvide;
	word32_t &VSizeReq;
	word32_t &VStatsRx;
	word32_t &VStatsTx;
	CPackListHandler &VTCPackList;
	CPackListHandler &VTMPackList;
	int32_t &VTryNumber;


	//Pool

	class CEDROOMPOOLCDTMTCCommServer:public CEDROOMProtectedMemoryPool {
		public:
		CEDROOMPOOLCDTMTCCommServer(TEDROOMUInt32 elemCount,CDTMTCCommServer *pMem, TEDROOMBool *pMarks);
		CDTMTCCommServer	* AllocData();
	};
	CEDROOMPOOLCDTMTCCommServer	& EDROOMPoolCDTMTCCommServer;
	class CEDROOMPOOLCDDataBurst:public CEDROOMProtectedMemoryPool {
		public:
		CEDROOMPOOLCDDataBurst(TEDROOMUInt32 elemCount,CDDataBurst *pMem, TEDROOMBool *pMarks);
		CDDataBurst	* AllocData();
	};
	CEDROOMPOOLCDDataBurst	& EDROOMPoolCDDataBurst;
	class CEDROOMPOOLCDBurst:public CEDROOMProtectedMemoryPool {
		public:
		CEDROOMPOOLCDBurst(TEDROOMUInt32 elemCount,CDBurst *pMem, TEDROOMBool *pMarks);
		CDBurst	* AllocData();
	};
	CEDROOMPOOLCDBurst	& EDROOMPoolCDBurst;
	class CEDROOMPOOLTEDROOMWord16:public CEDROOMProtectedMemoryPool {
		public:
		CEDROOMPOOLTEDROOMWord16(TEDROOMUInt32 elemCount,TEDROOMWord16 *pMem, TEDROOMBool *pMarks);
		TEDROOMWord16	* AllocData();
	};
	CEDROOMPOOLTEDROOMWord16	& EDROOMPoolTEDROOMWord16;


	// CONSTRUCTORES

	EDROOMTopContext (CASGI &act, word32_t & EDROOMpVarVAcks, byte_t & EDROOMpVarVAltBit, bool_t & EDROOMpVarVAlternate, CDBurst & EDROOMpVarVBurst, CDDataBurst & EDROOMpVarVDataBurst, CPackListHandler & EDROOMpVarVFreePackList, bool_t & EDROOMpVarVIsSatControl, bool_t & EDROOMpVarVIsSenTMTCDelivery, bool_t & EDROOMpVarVLastTCBurst, bool_t & EDROOMpVarVLastTMBurst, bool_t & EDROOMpVarVLinkLost, word32_t & EDROOMpVarVSizeProvide, word32_t & EDROOMpVarVSizeReq, word32_t & EDROOMpVarVStatsRx, word32_t & EDROOMpVarVStatsTx, CPackListHandler & EDROOMpVarVTCPackList, CPackListHandler & EDROOMpVarVTMPackList, int32_t & EDROOMpVarVTryNumber, CEDROOMPOOLCDTMTCCommServer & EDROOMpPoolCDTMTCCommServer, CEDROOMPOOLCDDataBurst & EDROOMpPoolCDDataBurst, CEDROOMPOOLCDBurst & EDROOMpPoolCDBurst, CEDROOMPOOLTEDROOMWord16 & EDROOMpPoolTEDROOMWord16 );

	EDROOMTopContext ( EDROOMTopContext &contex );

public:

	bool EDROOMSearchContextTrans(TEDROOMTransId &edroomCurrentTrans);

	void EDROOMNuevoMens () { EDROOMcomponent.EDROOMNewMsg(); }

	// Funciones Definidas por el usuario

	void	FBurstError();

	word16_t	FCalculateTMToCopy();

	void	FCalculateTMToProvide();

	void	FCheckTCAcks(bool_t *pAcksOk, bool_t *pAllOk, byte_t nNum, CPackListHandler & packList);

	void	FCommInterrupted();

	void	FCopyList();

	void	FInitCommConf();

	void	FKeepOnComm();

	void	FProgSATReady();

	void	FReceiveTC(CDDataBurst dataBurst);

	void	FRequestTCList(word32_t &VSizeProv);

	void	FRequestTCList();

	void	FSendReady();

	void	FSendTCPacketsToTMTCServer();

	void	FStoreReceiveTC(CDDataBurst & dataBurst);

	void	FToComm();

	void	FToStandby();

	bool_t	GEnConstruccion();

	bool_t	GIsSatControl();

	bool_t	GIsSenTMTCDelivery();

	bool_t	GLinkLost();

};

// ***********************************************************************

// class EDROOM_CTX_SATControl_3

// ***********************************************************************



class EDROOM_CTX_SATControl_3 : public EDROOMTopContext {

protected:
	enum edroomStateId{WaitTCFrames, SendingTM, CommInterrupted, TxTMCompleted};

	enum IdTrans{EntryToSATControl, EntryToSATControl_TCNotCompleted, EntryToSATControl_TCCompleted, TxTCAcksRxTCFrames, TxTCAcksRxTCFrames_TCCompleted, TxTCAcksRxTCFrames_TCNotCompleted, TxTCAcksRxTCFrames_LinkLost, TxTMFramesRxTMAck, TxTMFramesRxTMAck_TMFramesPending, TxTMFramesRxTMAck_LinkLost, TxTMFramesRxTMAck_NoTMFramesPending,  EDROOMMEMORIA };

	//Constantes



	//Variables



	// CONSTRUCTORES

	EDROOM_CTX_SATControl_3( EDROOMTopContext &contex );

	EDROOM_CTX_SATControl_3( EDROOM_CTX_SATControl_3 &contex );

public:
	bool EDROOMSearchContextTrans(TEDROOMTransId &edroomCurrentTrans);

	// Funciones Definidas por el usuario

	void	FProgSATTC();

	void	FProgSATTM();

	void	FSendTCAck();

	void	FSendTM();

	void	FTxTMCompleted();

	bool_t	GIsNotLastTCBurst();

	bool_t	GLastTMBurst();

};

// ***********************************************************************

// class EDROOM_CTX_SenTMTCDelivery_4

// ***********************************************************************



class EDROOM_CTX_SenTMTCDelivery_4 : public EDROOMTopContext {

protected:
	enum edroomStateId{Accept, Loading, TMTCDeliveryCompleted, CommInterrupted};

	enum IdTrans{EntryToTmTcDelivery, SendAccept, SendAccept_NotOk, SendAccept_LinkLost, SendAccept_ReceiveOk, Load, Load_NotCompleted, Load_Completed, Load_LinkLost,  EDROOMMEMORIA };

	//Constantes



	//Variables

	BurstTypeReceived_t &VBurstType;
	bool_t &VSendDel;


	// CONSTRUCTORES

	EDROOM_CTX_SenTMTCDelivery_4( EDROOMTopContext &contex, BurstTypeReceived_t & EDROOMpVarVBurstType, bool_t & EDROOMpVarVSendDel );

	EDROOM_CTX_SenTMTCDelivery_4( EDROOM_CTX_SenTMTCDelivery_4 &contex );

public:
	bool EDROOMSearchContextTrans(TEDROOMTransId &edroomCurrentTrans);

	// Funciones Definidas por el usuario

	void	FProgAccept();

	void	FProgSenLoad();

	void	FSendAccept();

	void	FSendDown();

	void	FTxTMTCDeliveryCompleted();

	bool_t	GNotCompleted();

	bool_t	GResponseAccept();

};

// ***********************************************************************

// class EDROOM_SUB_SATControl_3

// ***********************************************************************



class EDROOM_SUB_SATControl_3:public EDROOM_CTX_SATControl_3{ 

protected:



	EDROOM_CTX_SATControl_3::edroomStateId edroomCurrentState;

	EDROOM_CTX_SATControl_3::edroomStateId edroomNextState;

	//Variables

public:

	EDROOMTopContext::edroomStateId idEstado;

	// CONSTRUCTOR*************************************************

	EDROOM_SUB_SATControl_3(EDROOMTopContext::edroomStateId idEst, EDROOMTopContext &contex );

	// llegada*************************************************

	TEDROOMTransId llegada(EDROOMTopContext::IdTrans );

	// Inicio**********************************************

	void EDROOMInicio();

	// ***********************************************************************

	// SubEstado Hoja WaitTCFrames

	// ***********************************************************************



	TEDROOMTransId EDROOMWaitTCFramesllegada();

	// ***********************************************************************

	// SubEstado Hoja SendingTM

	// ***********************************************************************



	TEDROOMTransId EDROOMSendingTMllegada();

	// ***********************************************************************

	// SubEstado Hoja CommInterrupted

	// ***********************************************************************



	TEDROOMTransId EDROOMCommInterruptedllegada();

	// ***********************************************************************

	// SubEstado Hoja TxTMCompleted

	// ***********************************************************************



	TEDROOMTransId EDROOMTxTMCompletedllegada();

};

// ***********************************************************************

// class EDROOM_SUB_SenTMTCDelivery_4

// ***********************************************************************



class EDROOM_SUB_SenTMTCDelivery_4:public EDROOM_CTX_SenTMTCDelivery_4{ 

protected:



	EDROOM_CTX_SenTMTCDelivery_4::edroomStateId edroomCurrentState;

	EDROOM_CTX_SenTMTCDelivery_4::edroomStateId edroomNextState;

	//Variables

	BurstTypeReceived_t VBurstType;
	bool_t VSendDel;
public:

	EDROOMTopContext::edroomStateId idEstado;

	// CONSTRUCTOR*************************************************

	EDROOM_SUB_SenTMTCDelivery_4(EDROOMTopContext::edroomStateId idEst, EDROOMTopContext &contex );

	// llegada*************************************************

	TEDROOMTransId llegada(EDROOMTopContext::IdTrans );

	// Inicio**********************************************

	void EDROOMInicio();

	// ***********************************************************************

	// SubEstado Hoja Accept

	// ***********************************************************************



	TEDROOMTransId EDROOMAcceptllegada();

	// ***********************************************************************

	// SubEstado Hoja Loading

	// ***********************************************************************



	TEDROOMTransId EDROOMLoadingllegada();

	// ***********************************************************************

	// SubEstado Hoja TMTCDeliveryCompleted

	// ***********************************************************************



	TEDROOMTransId EDROOMTMTCDeliveryCompletedllegada();

	// ***********************************************************************

	// SubEstado Hoja CommInterrupted

	// ***********************************************************************



	TEDROOMTransId EDROOMCommInterruptedllegada();

};

// ***********************************************************************

// class EDROOMTopState

// ***********************************************************************



class EDROOMTopState : public EDROOMTopContext {

protected:

	EDROOM_SUB_SATControl_3 EDROOMSATControl;
	EDROOM_SUB_SenTMTCDelivery_4 EDROOMSenTMTCDelivery;


	EDROOMTopContext::edroomStateId edroomCurrentState;

	EDROOMTopContext::edroomStateId edroomNextState;

	//Variables

	word32_t VAcks;
	byte_t VAltBit;
	bool_t VAlternate;
	CDBurst VBurst;
	CDDataBurst VDataBurst;
	CPackListHandler VFreePackList;
	bool_t VIsSatControl;
	bool_t VIsSenTMTCDelivery;
	bool_t VLastTCBurst;
	bool_t VLastTMBurst;
	bool_t VLinkLost;
	word32_t VSizeProvide;
	word32_t VSizeReq;
	word32_t VStatsRx;
	word32_t VStatsTx;
	CPackListHandler VTCPackList;
	CPackListHandler VTMPackList;
	int32_t VTryNumber;


	//Pools

	CEDROOMPOOLCDTMTCCommServer	EDROOMPoolCDTMTCCommServer;
	CEDROOMPOOLCDDataBurst	EDROOMPoolCDDataBurst;
	CEDROOMPOOLCDBurst	EDROOMPoolCDBurst;
	CEDROOMPOOLTEDROOMWord16	EDROOMPoolTEDROOMWord16;


public:

	// CONSTRUCTOR

	EDROOMTopState (CASGI &act, CEDROOMMemory *pEDROOMMemory  );


	// EDROOMBehaviour**********************************************

	void EDROOMBehaviour();

	// Inicio**********************************************

	void EDROOMInicio();

	// ***********************************************************************

	// SubEstado Inicial 

	// ***********************************************************************



	TEDROOMTransId EDROOMIllegada();

	// ***********************************************************************

	// SubEstado Hoja StandBy

	// ***********************************************************************



	TEDROOMTransId EDROOMStandByllegada();

	// ***********************************************************************

	// SubEstado Hoja SendingSATReady

	// ***********************************************************************



	TEDROOMTransId EDROOMSendingSATReadyllegada();

	// ***********************************************************************

	// SubEstado Hoja CommInterrupted

	// ***********************************************************************



	TEDROOMTransId EDROOMCommInterruptedllegada();

};


#endif
